#!/bin/sh

if [ $# -lt 1 ]; then
    echo "\
usage: deflac <dir> [options...]
  - option bitrate '-b': deflac <dir> [-b<bitrate>]

example: deflac . -b320"
    exit 1
fi

bitrate=320

for arg in "$@"; do
    case ${arg} in
        -b)
            bitrate=${arg}
    esac
done

for file in "$(readlink -f "$1")"/*.flac; do
    echo "${file} -> ${bitrate}kbps CBR mp3"
    base="$(basename "${file}" .flac)"
    ffmpeg -i "${file}" -ab "${bitrate}"k -map_metadata 0 -id3v2_version 3 "$(readlink -f "$1")/${base}.mp3"
done
